<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Exception;


/**
 * 地区接口控制器
 */
class Region extends AdminBase
{
    //protected $regionLogic;
    protected $region_model;
    protected function _initialize()
    {
        parent::_initialize();
        //$this->regionLogic = logic("Region");
        $this->region_model = model('Region');
    }

    /**
     * 地区列表
     */
    public function index()
    {
        $parent_id = $this->request->param('parent_id', 0, 'intval');
        $region_name = $this->request->param('regio_name', '', 'app\common\controller\ParamFilter::normalParam');
        $regions = $this->region_model->getListByPid($parent_id, $region_name);


        $parent = $this->region_model->getInfo(['region_id' => $parent_id]);

        $assign = array(
            'regions' => $regions,
            'parent_id' => $parent_id,
            'parent' => $parent
        );
        $this->assign($assign);
        return $this->fetch('index', [
            'regions' => $regions,
            'parent' => $parent,
            'parent_id' => $parent_id,
        ]);
    }

    /**
     * 获取指定地区的全部子级地区信息
     */
    public function get_son_regions()
    {
        try {
            $region_id = $this->request->param('region_id', 1, 'intval');
            if ($region_id <= 0) {
                exception("缺少必要的参数！");
            }
            return $this->out($this->regionLogic->getSonRegionsByParentId($region_id));
        } catch (Exception $e) {
            $errmsg = $e->getMessage();
            $errmsg = $errmsg ? $errmsg : '操作失败，请重试！';
            return $this->errmsg($errmsg);
        }

    }

    public function edit()
    {
        $region_id = $this->request->param('region_id', 0, 'intval');
        $region_info = $this->region_model->getInfo([
            'region_id' => $region_id,
        ]);
        if ($this->request->isAjax() && $region_id > 0) {
            if (empty($region_info)) {
                return ajax_return_error('指定记录不存在,请刷新页面后重试！');
            }

            $region_name = $this->request->param('region_name', '', 'app\common\controller\ParamFilter::normalParam');
            if (mb_strlen($region_name) > 30) {
                return ajax_return_error('请输入正确的地区名字！');
            }

            $first_char = $this->request->param('first_char', '', 'trim');
            if (!preg_match('/^[a-zA-Z]$/', $first_char)) {
                return ajax_return_error('请输入正确的首字母！');
            }

            $sort_order = $this->request->param('sort_order', 0, 'intval');
            if ($sort_order < 0) {
                $sort_order = 0;
            }

            if ($sort_order > 255) {
                $sort_order = 255;
            }

            $data = [
                'region_name' => $region_name,
                'sort_order'  => $sort_order,
                'first_char'  => $first_char,
            ];

            $res = $this->region_model->saveData($region_id, $data);
            if ($res === false) {
                return ajax_return_error('修改失败，请重试!');
            }

            return ajax_return_adv('修改成功！');
        }

        return $this->fetch('edit', [
            'region_info' => $region_info,
        ]);
    }

    /**
     * ajax异步获取省市区信息
     * @return string
     */
    public function get_region_by_parent_id()
    {
        if ($this->request->isAjax()) {
            $parent_id = $this->request->param('pid', 1, 'intval');
            $list = $this->region_model->getArrayListByPid($parent_id, 'region_id,region_name');
            return ajax_return($list);
        }
    }
}